package com.lineage.data.item_etcitem.event;

import com.lineage.data.event.LeavesSet;
import com.lineage.data.executor.ItemExecutor;
import com.lineage.data.item_etcitem.brave.StatusBrave;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_PacketBoxExp;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.ServerBasePacket;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class GemOfDragon extends ItemExecutor {
  private static final Log _log = LogFactory.getLog(StatusBrave.class);
  
  private int _percentage = 100;
  
  private int _gfxid = 0;
  
  private boolean _isCrown;
  
  private boolean _isKnight;
  
  private boolean _isElf;
  
  private boolean _isWizard;
  
  private boolean _isDarkelf;
  
  private boolean _isDragonKnight;
  
  private boolean _isIllusionist;
  
  public static ItemExecutor get() {
    return new GemOfDragon();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    if (item == null)
      return; 
    if (pc == null)
      return; 
    if (!LeavesSet.START) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fT尚未开放"));
      return;
    } 
    if (pc.getlogintime() >= 960000) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fT殷海萨的祝福:已达饱和.无法在使用"));
      return;
    } 
    int itemId = item.getItemId();
    if (check(pc)) {
      pc.getInventory().removeItem(item, 1L);
      int exp = pc.getlogintime();
      int addexp = exp + this._percentage * LeavesSet.EXP;
      pc.setlogintime(addexp);
      if (pc.getlogintime() > 960000)
        pc.setlogintime(960000); 
      if (this._gfxid > 0)
        pc.sendPacketsX8((ServerBasePacket)new S_SkillSound(pc.getId(), this._gfxid)); 
      if (exp != pc.getlogintime())
        pc.sendPackets((ServerBasePacket)new S_PacketBoxExp(pc.getlogintime() / LeavesSet.EXP)); 
    } 
  }
  
  private boolean check(L1PcInstance pc) {
    try {
      if (pc.isCrown() && this._isCrown)
        return true; 
      if (pc.isKnight() && this._isKnight)
        return true; 
      if (pc.isElf() && this._isElf)
        return true; 
      if (pc.isWizard() && this._isWizard)
        return true; 
      if (pc.isDarkelf() && this._isDarkelf)
        return true; 
      if (pc.isDragonKnight() && this._isDragonKnight)
        return true; 
      if (pc.isIllusionist() && this._isIllusionist)
        return true; 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
    return false;
  }
  
  private void set_use_type(int use_type) {
    try {
      if (use_type >= 64) {
        use_type -= 64;
        this._isIllusionist = true;
      } 
      if (use_type >= 32) {
        use_type -= 32;
        this._isDragonKnight = true;
      } 
      if (use_type >= 16) {
        use_type -= 16;
        this._isDarkelf = true;
      } 
      if (use_type >= 8) {
        use_type -= 8;
        this._isWizard = true;
      } 
      if (use_type >= 4) {
        use_type -= 4;
        this._isElf = true;
      } 
      if (use_type >= 2) {
        use_type -= 2;
        this._isKnight = true;
      } 
      if (use_type >= 1) {
        use_type--;
        this._isCrown = true;
      } 
      if (use_type > 0)
        _log.error("StatusBrave 可执行职业设定错误:余数大于0"); 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void set_set(String[] set) {
    try {
      this._percentage = Integer.parseInt(set[1]);
      if (this._percentage <= 0) {
        _log.error("GemOfDragon 设置错误:恢复百分比小于等于0! 使用预设100");
        this._percentage = 100;
      } 
    } catch (Exception exception) {}
    try {
      this._gfxid = Integer.parseInt(set[2]);
    } catch (Exception exception) {}
    try {
      int user_type = Integer.parseInt(set[3]);
      set_use_type(user_type);
    } catch (Exception exception) {}
  }
}
